<?php
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
require('D:/Projects/gagcrawler/includes/functions.php');
require('D:/Projects/gagcrawler/includes/simple_html_dom.php');
$url = 'http://www.haivl.com/photo/591872';
function getHaivlVideo($url){
	$ch = curl_init();

	$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
	$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	$header[] = "Cache-Control: max-age=0";
	$header[] = "Connection: keep-alive";
	$header[] = "Keep-Alive: 300";
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$header[] = "Accept-Language: en-us,en;q=0.5";
	$header[] = "Pragma: "; // browsers keep this blank.
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	//curl_setopt($ch, CURLOPT_PROXY, '210.245.0.24:80');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	// grab URL and pass it to the browser
	$responseContent = curl_exec($ch);
	//die($responseContent);
	$html = str_get_html($responseContent);
	$videoCode = '';
	foreach($html->find('div.photoImg') as $element){
		$iframe = $element->find('iframe', 0);
		//die($iframe->src);
		if($iframe->src != ''){
			$videoCode = $iframe->src;
			break;
		}
	}
	return $videoCode;
}
require('D:/Projects/gagcrawler/dbconnect.php');
$rsFind = mysql_query("SELECT id,original_url FROM data WHERE item_type='video' AND video_code = '' AND source_page='haivl.com'");
while($rowFind = mysql_fetch_array($rsFind)){
	echo "Processing ID[".$rowFind['id']."] ..\t";
	//die('http://haivl.com/'.$rowFind['original_url']);
	$videoCode = addslashes(getHaivlVideo('http://www.haivl.com'.$rowFind['original_url']));
	if($videoCode != '' && strlen($videoCode) < 200 && strstr($videoCode, 'youtube')){
		if(mysql_query("UPDATE data SET video_code = '$videoCode' WHERE id='".$rowFind['id']."'")){
			echo "done\n";
		}
		else{
			echo "failed\n";
		}
	}
	else{
		echo "failed\n";
	}
	//sleep(1);
}